﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Phone.Net.NetworkInformation;
using System.Diagnostics;
using System.ServiceModel;
using System.Globalization;
using QuickTasks.FileStoreService;

namespace QuickTasks
{
    public class Exceptions
    {
        private static bool isSendErrors;
        public static bool IsSendErrors
        {
            get
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("SendErrors"))
                {
                    if (bool.Parse(settings["SendErrors"].ToString()))
                        isSendErrors = true;
                    else
                        isSendErrors = false;
                }
                return isSendErrors;
            }
        }

        private static bool isNetworkAvailable;
        public static bool IsNetworkAvailable
        {
            get
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    if (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None)
                        isNetworkAvailable = true;
                    else
                        isSendErrors = false;
                }
                return isNetworkAvailable;
            }
        }

        
        public static void SaveOrSendExceptions(string body, Exception data)
        {
            body = body + "\n\n" + data.Message + "\n\n Stack Trace:- " + data.StackTrace + "\n Extra Data \n";
           
            foreach (DictionaryEntry d in data.Data)
                body = body + d.Key + " -> " + d.Value + "\n";
            body = body + BuildMailMessage();
            SaveData(body);
        }

        public static void SaveData(string body)
        {
            try
            {
                
                if (Exceptions.IsSendErrors && Exceptions.IsNetworkAvailable)
                {
                    Service1Client service = new Service1Client();
                    service.SendMailAsync("QuickTasksUserExceptions@hotmail.com", "wp7errors@lartsoft.com", "QuickTasks Errors", body);
                    service.SendMailCompleted += new EventHandler<SendMailCompletedEventArgs>(service_SendMailCompleted);
                }
            }
            catch (Exception ex)
            {
                string exNew = "Exception in SaveData Method In Exceptions file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
                
            }
        }

        static void service_SendMailCompleted(object sender, SendMailCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //FaultException fault = e.Error as FaultException;
                
            }
        }

       

        public static string BuildMailMessage()
        {
            string body = "\n\n\nDateTime : " + System.DateTime.Now +
            "\nCulture : " + CultureInfo.CurrentCulture.Name +
            "\nDevice Name : " + ExtendedPropertyHelper.GetDeviceModel() +
            "\nStandard Time : " + TimeZoneInfo.Local.StandardName +
            "\nManufacturer : " + ExtendedPropertyHelper.GetManufacturer();
            return body;
        }
    }
}
